package com.bridgeintelligent.tag.engine.mvc;

import com.bridgeintelligent.tag.engine.api.*;
import com.bridgeintelligent.tag.engine.api.rsp.CommonRsp;
import com.bridgeintelligent.tag.engine.core.cache.CacheConfig;
import com.bridgeintelligent.tag.engine.core.cache.CacheUtils;
import com.bridgeintelligent.tag.engine.core.cache.InitCache;
import com.bridgeintelligent.tag.engine.core.refresh.EngineRefreshApi;
import com.bridgeintelligent.tag.engine.core.refresh.EngineRefreshApiUtils;
import com.bridgeintelligent.tag.engine.core.refresh.EngineRefreshConfig;
import com.bridgeintelligent.tag.mapper.DimDataMapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * Created by JackCheung on 2019/10/21 8:11 PM.
 */
@AllArgsConstructor
@Service
public class EngineUtilsService {

    private DimDataMapper dimDataMapper;
    private EngineRefreshConfig engineRefreshConfig;
    private EngineRefreshApiUtils engineRefreshApiUtils;

    /**
     * 根据主题获取索引最新数据日期
     *
     * @return CommonRsp
     */
    public CommonRsp date(String themeId) {
        List<IndexNameInfo> list = dimDataMapper.findDate();
        List<IndexNameInfo> listMatch = list.stream()
                                            .filter(indexNameInfo -> indexNameInfo.getId()
                                                                                  .equals(themeId))
                                            .collect(Collectors.toList());
        if (listMatch != null && listMatch.size() > 0) {
            return new CommonRsp<>(BaseCode.SUCCESS, StringUtils.substringAfterLast(listMatch.get(0)
                                                                                             .getDataDate(), "_"));
        }
        return null;
    }

    /**
     * 刷新所有应用服务器的缓存
     *
     * @return CommonRsp
     */
    public CommonRsp refreshAll() {
        String urls = engineRefreshConfig.getRequestUrls();
        String[] urlArray = urls.split(";");
        for (String str : urlArray) {
            EngineRefreshApi engineRefreshApi = engineRefreshApiUtils.build(EngineRefreshApi.class, str + EngineConfig.REFRESH_ITEM_PATH);
            engineRefreshApi.refreshItem();
        }
        return new CommonRsp<>(BaseCode.SUCCESS);
    }

    /**
     * 刷新单台服务器的缓存
     *
     * @return CommonRsp
     */
    public CommonRsp refreshItem() {
        initDimInfo();
        initDimItemInfo();
        initConfigurationInfo();
        return new CommonRsp<>(BaseCode.SUCCESS);
    }

    /**
     * 刷新标签缓存
     */
    private void initDimInfo() {
        List<DimInfo> list = dimDataMapper.findDimDataList();
        Map<String, DimInfo> map = new ConcurrentHashMap<>(list.size());
        list.forEach(dimInfo -> map.put(dimInfo.getDimId(), dimInfo));
        CacheUtils.refreshCache(CacheConfig.DIM_INFO, map);
    }

    /**
     * 刷新标签项目缓存
     */
    private void initDimItemInfo() {
        List<DimItemInfo> list = dimDataMapper.findDimItemList();
        Map<String, DimItemInfo> map = new ConcurrentHashMap<>(list.size());
        list.forEach(dimItemInfo -> map.put(dimItemInfo.getGroupId(), dimItemInfo));
        CacheUtils.refreshCache(CacheConfig.DIM_ITEM_INFO, map);
    }

    /**
     * 刷新索引配置缓存
     */
    private void initConfigurationInfo() {
        List<IndexNameInfo> indexNameInfoList = dimDataMapper.findDate();
        Map<String, IndexNameInfo> map = new ConcurrentHashMap<>(indexNameInfoList.size());
        indexNameInfoList.forEach(indexNameInfo -> map.put(indexNameInfo.getId(), indexNameInfo));
        CacheUtils.refreshCache(CacheConfig.CONFIGURATION_INFO, map);
    }
}
